package net.icontents.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.icontents.beans.component.ArticleComponent;
import net.icontents.dao.ArticleComponentDao;

import org.springframework.stereotype.Repository;

@Repository
public class ArticleComponentDaoImpl extends BaseORMDaoImpl<ArticleComponent> implements ArticleComponentDao {

	public ArticleComponentDaoImpl(){
		setNamespace("ArticleComponent");
	}

	@Override
	public List<ArticleComponent> selectByArticleId(long articleId, int position) {
        Map<String,Object> params = new HashMap<String,Object>(2);
        params.put("articleId",articleId);
        params.put("position",position);
        System.out.println("params:"+params);
        return queryForEntryList("selectByArticleId",params);
	}

	@Override
	public void deleteByArticleId(int articleId) {
        Map<String,Object> params = new HashMap<String,Object>(1);
        params.put("articleId",articleId);
		
        delete("deleteByArticleId", params);
	}
	
	public ArticleComponent getArticleComponentById(int articleId,int sequence,int position){
		Map<String,Object> params = new HashMap<String,Object>(3);
		
		params.put("articleId",articleId);
        params.put("sequence",sequence);
        params.put("position",position);
        
        ArticleComponent articleComponent = queryForEntry("getArticleComponentById", params);
        
        ArticleComponent newArticleComponent = null;
		
		System.out.println("=================================>"+articleComponent);
		try {
            newArticleComponent = (ArticleComponent)(Class.forName(ArticleComponent.class.getPackage().getName()+"." + articleComponent.getComponentType()).getConstructor(new Class[]{ArticleComponent.class}).newInstance(articleComponent));
        } catch (Exception e1) {
            newArticleComponent = articleComponent;
        }
        return newArticleComponent;
	}
}
